from enetity_extraction.test import extract_information
from enetity_extraction.json_result import generate_return_json
import re
# 金额格式检查，统一为三位小写书写加小数点后两位，目前统一到123,456.78元
def money_format(extraction_result):
    check_result = []


    if len(extraction_result["金额"]) != 0:
        for org_money in extraction_result["金额"]:
             mmoney = org_money['match']
             money_folat = to_folat(mmoney)


             money = to_money_format(money_folat)
             if mmoney != str(money):
                 check_result.append(generate_return_json(paragraph_index=org_money['paragraph_index'],
                                                          start=org_money['start_pos'],
                                                          end=org_money['end_pos'],
                                                          explain='金额格式有误',
                                                          correct=money,
                                                          word=org_money['match']))



        return check_result

# 单位元，万元，处理，转换为float
def to_folat(org_money):
    # 单位：元，1345元
    if org_money[-1] == "元":
        money_data_1 = org_money[:-1]
        money_data_2 = org_money[:-1]
        # 单位：万元，1345万元
        if money_data_1[-1] == "万":

            money_data_1 = money_data_1[:-1]
            money_data_2 = re.sub(",", '', money_data_1)
            money_data_2 = float(money_data_2) * 10000

            return money_data_2

        money_data_1 = re.sub(",",'',money_data_1)
        # money_data_1 = round((float(money_data_1)),2)
        money_data_1 = float(money_data_1)

        return money_data_1


    # else:
    #     return org_money

def to_money_format(money_data1):

    # money_data = money_data1.__format__(",")
    # money_data = format(money_data1,'.2f')
    money_data = format(money_data1,",")
    if money_data[-2] =='.':
        money_data = money_data +'0'
    money_data =money_data + "元"
    return money_data

if __name__ == '__main__':
    #
    input_text = "1、判令被告向原告立即返还12,000.00元本金；2、判令被告杨铧向原告支付至2021年1月13日的利息249.35元"
    input_text_list = input_text.split('\n')
    result_list = []
    extraction_result = extract_information(input_text_list)
    result_list = money_format(extraction_result)
    print(result_list)


    # result_list.extend(money_format(extraction_result))
    # print(result_list)
